home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Make / make.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  8KB  |  283 lines

  1. /* make.c */
  2.  
  3. #include "rules.h"
  4. #include "vars.h"
  5. #include <stdio.h>
  6. #include <libraries/dos.h>
  7.  
  8. int debug;
  9. long int days, mins, secs;
  10.  
  11. main( argc, argv )
  12.    int argc;
  13.    char **argv;
  14. {
  15.    FILE *MakeFile;
  16.    int i,j;
  17.  
  18.    printf("Make V1.0 (c) 1985 by General Overall Design.\n");
  19.    printf("May (only) be distributed free of charge.\n");
  20.    /* General Overall Design
  21.     * P.O. Box 2039
  22.     * W. Lavayette IN 47906
  23.     */
  24.    if( argc > 2 && strcmp(argv[1], "-f")==0 ){       /* make -f file ... */
  25.       MakeFile = fopen( argv[2] , "r" );
  26.       argc -= 2;
  27.       argv += 2;
  28.    } else
  29.       MakeFile = fopen("makefile" , "r");
  30.  
  31.    if( argc > 1 && strcmp( argv[1], "-d" ) == 0 ){
  32.       debug=1;
  33.       argc--;
  34.       argv++;
  35.       printf("debug on.\n");
  36.    }
  37.  
  38.    if( MakeFile == NULL ){                  /* open failed  */
  39.       printf("make: can't open makefile\n");
  40.       exit( 1 );
  41.    }
  42.    if ( debug )
  43.       printf("makefile open\n");
  44.  
  45.    Parse( MakeFile );
  46.  
  47.    if( debug ){
  48.       printf("Variables:\n");
  49.       for( i=0 ; i < n_Vars ; i++ )
  50.          printf( "%s = '%s'\n", avr_Vars[i].pch_name , avr_Vars[i].string );
  51.       printf("Rules:\n");
  52.       for( i=0 ; i < n_Rules ; i++ ){
  53.          printf( "%s : ", arl_Rules[i].pch_target );
  54.          for( j = 0; j < arl_Rules[i].c_depend; j++ )
  55.             printf("'%s' ", arl_Rules[i].apch_depend[ j ] );
  56.          printf("\n");
  57.          for( j = 0; j < arl_Rules[i].c_action; j++ )
  58.             printf("%s\n", arl_Rules[i].apch_action[j] );
  59.       }
  60.    }
  61.  
  62.    if( argc == 1 )
  63.       make( arl_Rules[0].pch_target, &days, &mins, &secs );
  64.    else
  65.       while( --argc != 0 )
  66.          make( *++argv, &days, &mins, &secs );
  67.  
  68.    printf("done.\n");
  69. }
  70.  
  71. static char *GetLine(), *NextWord(), *malloc();
  72.  
  73. Parse( fd )
  74.     FILE *fd;
  75. {
  76.     char *pch_Position, *pch_Word, *pch_Target;
  77.  
  78.     pch_Position = GetLine( fd );
  79.     while( pch_Position ){
  80.         if( debug )
  81.            printf("Parsing:%s\n", pch_Position );
  82.         if( '#' == *pch_Position ){
  83.             pch_Position = GetLine(fd);
  84.             continue;
  85.         }
  86.         pch_Word = pch_Position;
  87.         pch_Position = NextWord( pch_Position );
  88.         if( *pch_Position == '=' ){
  89.             DefineVar( pch_Word, ++ pch_Position );
  90.             pch_Position = GetLine( fd );
  91.         } else {
  92.             pch_Target = pch_Word;
  93.             pch_Word = pch_Position = NextWord( pch_Position );
  94.             pch_Position = NextWord( pch_Position );
  95.             while( pch_Word ){
  96.                  AddDependant( pch_Word, pch_Target );
  97.                  pch_Word = pch_Position;
  98.                  pch_Position = NextWord( pch_Position );
  99.             }
  100.             pch_Position = GetLine( fd );
  101.             while( ' ' == *pch_Position ){
  102.                  AddAction( pch_Position , pch_Target );
  103.                  pch_Position = GetLine( fd );
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109. char *
  110. NextWord( pch )
  111.     char *pch;
  112. {
  113.     if( NULL == pch || NULL == *pch )
  114.         return NULL;
  115.     while( *pch && *pch != ' ' && *pch != '\t' && *pch != ',' )
  116.         pch++;
  117.     if( *pch == '\0' )
  118.         return NULL;
  119.     else
  120.         *pch = '\0';
  121.     do
  122.         pch++;
  123.     while ( *pch && (' ' == *pch || '\t' == *pch || ',' == *pch ));
  124.     if(*pch == '\0')
  125.         return NULL;
  126.     else
  127.         return pch;
  128. }
  129.  
  130. /* states for GetLine */
  131. #define HALT        -1
  132. #define IGNOREWHITE  0
  133. #define START        1
  134. #define NONWHITE     2
  135. #define BACKSLASH    3
  136. #define VARIABLE     4
  137. char *names[]={"ignorewhite","start","nonwhite","backslash","variable"};
  138.  
  139. static char *
  140. GetLine( fd )
  141.    FILE *fd;
  142. {
  143.    static char buf[ 1024 ];
  144.    auto int pos = 0;
  145.    auto int state = START;
  146.    int ch;
  147.    char *pch_var, *pch_replace;
  148.  
  149.    ch = getc(fd);
  150.    if( EOF == ch )
  151.       return NULL;
  152.    while( state != HALT ){
  153.        if( debug )
  154.           printf( "state: %s\n", names[state]);
  155.        switch( state ){
  156.        case IGNOREWHITE:
  157.                   if ( ' ' == ch || '\t' == ch || ',' == ch )
  158.                       ch = getc(fd);
  159.                   else
  160.                       state = START;
  161.                   break;
  162.        case START:
  163.                   if( ' ' == ch || '\t' == ch || ',' == ch ) {
  164.                       buf[pos++] = ch;
  165.                       ch = getc(fd);
  166.                       state = IGNOREWHITE;
  167.                       break;
  168.                   } else if( '$' == ch ) {
  169.                       state = VARIABLE;
  170.                       ch = getc(fd);
  171.                       break;
  172.                   } else if( '\\' == ch ) {
  173.                       ch = getc(fd);
  174.                       state = BACKSLASH;
  175.                       break;
  176.                   } else if( '\n' == ch || EOF == ch ) {
  177.                       state = HALT;
  178.                       break;
  179.                   } else {
  180.                       state = NONWHITE;
  181.                       break;
  182.                   }
  183.      case NONWHITE :
  184.                  if( ch != EOF && ch != '\n' && ch != '$' &&
  185.                      ch != '\\' && ch != ' ' && ch != '\t' && ch != ',' ) {
  186.                       buf[ pos++ ] = ch;
  187.                       ch = getc(fd);
  188.                  } else
  189.                       state = START;
  190.                  break;
  191.      case BACKSLASH:
  192.                  if( ch != '\n' ){
  193.                     buf[pos++] = '\\';
  194.                     buf[pos++] = ch;
  195.                  }
  196.                  ch = getc(fd );
  197.                  state = START;
  198.                  break;
  199.      case VARIABLE:
  200.                  if( '$' == ch ) {
  201.                     buf[pos++] = ch;
  202.                     state = START;
  203.                  } else if ( '{'== ch || '(' == ch ) {
  204.                     ch = getc(fd);
  205.                     pch_var = &buf[ pos ];
  206.                     while ( ')' != ch && '}' != ch ){
  207.                          buf[ pos++ ] = ch;
  208.                          ch = getc(fd);
  209.                     }
  210.                     ch = getc(fd);
  211.                     buf[pos]=NULL;
  212.                     pch_replace = VarLookup( pch_var );
  213.                     strcpy( pch_var , pch_replace );
  214.                     pos = strlen( buf );
  215.                     state = START;
  216.                  }
  217.                  break;
  218.          }
  219.      }
  220.      buf[pos] = NULL;
  221.      if( debug )
  222.         printf("buf='%s'\n", buf );
  223.      pch_replace = malloc( strlen(buf) + 1 );
  224.      if( pch_replace == NULL ){
  225.         printf("make: out of memory" );
  226.         exit(1);
  227.      }
  228.      strcpy( pch_replace , buf );
  229.      return pch_replace;
  230. }
  231.  
  232. make( pch_Target, mydays, mymins, mytics )
  233.    char *pch_Target;
  234.    long int *mydays, *mymins, *mytics;
  235. {
  236.    long int hisdays, hismins, histics;
  237.    int fl_Action;
  238.    char **depends, **actions;
  239.  
  240.    GetDate( pch_Target, mydays, mymins, mytics );
  241.    if( debug )
  242.       printf("make(): %s %ld %ld:%ld\n",pch_Target,*mydays,*mymins,*mytics );
  243.    depends = GetDependants( pch_Target );
  244.    actions = GetActions( pch_Target );
  245.    fl_Action = *mydays == 0 && *mymins == 0 && *mytics == 0;
  246.    while( *depends ){
  247.       if( make( *depends++, &hisdays, &hismins, &histics ) )
  248.          fl_Action = 1;
  249.       if( hisdays > *mydays || (hisdays == *mydays &&
  250.           ( hismins > *mymins || (hismins == *mymins && histics > *mytics ))))
  251.          fl_Action = 1;
  252.    }
  253.    if( fl_Action )
  254.       while( *actions ) {
  255.           printf("%s\n", *actions );
  256.           Execute( *actions, 0, 0 );
  257.           actions++;
  258.       }
  259.    return fl_Action;
  260. }
  261.  
  262. GetDate( pch_FileName, days, mins, secs )
  263.    char *pch_FileName;
  264.    long int *days, *mins, *secs;
  265. {
  266.    static struct FileInfoBlock buf;
  267.    char *p;
  268.  
  269.    if( p = (char *)Lock( pch_FileName, ACCESS_READ )){
  270.       if( Examine( p , &buf ) ) {
  271.          *days = buf.fib_Date.ds_Days;
  272.          *mins = buf.fib_Date.ds_Minute;
  273.          *secs = buf.fib_Date.ds_Tick;
  274.       } else
  275.          printf("Couldn't get info for %s\n", pch_FileName );
  276.       UnLock( p );
  277.    } else {
  278.       if ( debug )
  279.          printf("couldn't lock %s\n", pch_FileName );
  280.       *days = *mins = *secs = 0;
  281.    }
  282. }
  283.